%Program 11.1 Audio codec
%input: vector x of input signal
%output: vector out of output signal
%Example usage: out=simplecodec(cos((1:2^(12))*2*pi*256/2^(13)));
%               example signal is 1/2 sec. pure tone of frequency f = 256
function out=simplecoder5(x,b)
len=length(x);
n=2^5;                                % length of window
nw=floor(len/n);                      % number of length n windows in x
x=x(1:n*nw);                          % cut x to integer number of n windows
Fs=2^(13);                            % Fs=sampling rate
L=1;                           % b = quantization bits, [-L,L] amplitude range
q=2*L/(2^b-1);                        % q used for b bits on interval [-L, L]
for i=1:n                             % form the MDCT matrix
  for j=1:2*n
    M(i,j)= cos((i-1+1/2)*(j-1+1/2+n/2)*pi/n);
  end
end
M=sqrt(2/n)*M;
N=M';

out=[];
h=(sqrt(2)*sin(((1:2*n)-1/2)*pi/(2*n)))';
for k=1:nw-1                          % loop over length 2n windows
  x0=x(1+(k-1)*n:2*n+(k-1)*n)';
  x0=x0.*h;
  y0=M*x0;
  y1=round(y0/q);                     % transform components quantized
% Storage/transmission of file
  out=[out,y1];
end                                 % (of length 2n less than length of x)
[nr,nc] = size(out);
out = reshape(out,nr*nc,1);
end % END function